package com.anXin.article.controller;


import com.anXin.article.service.ArticleService;
import com.anXin.article.service.CollectService;
import com.anXin.article.service.LikeService;
import com.anXin.model.article.pojos.Article;
import com.anXin.model.article.pojos.Collect;
import com.anXin.model.article.pojos.Like;
import com.anXin.model.common.dtos.ResponseResult;
import com.anXin.model.common.enums.AppHttpCodeEnum;
import com.anXin.model.user.pojos.User;
import org.springframework.web.bind.annotation.*;
import utils.thread.AppThreadLocalUtil;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/app/like")
public class LikeController {


    @Resource
    private LikeService likeService;

    @Resource
    private ArticleService articleService;

    /**
     * 点赞
     * @param articleId
     * @return
     */

    @PostMapping("/add/{articleId}")
    public ResponseResult Collect(@PathVariable Long articleId){
   User user=AppThreadLocalUtil.getUser();

//       User user=new User();
//       user.setId(1302865306489733122L);

        Like like=new Like();
        like.setArticleId(articleId);
        like.setUserId(user.getId());
        Article article=articleService.query().eq("id",articleId).one();
        like.setBlogTitle(article.getTitle());
        likeService.save(like);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

    }


    /**
     * 取消点赞
     * @param articleId
     */

    @DeleteMapping("/delete/{articleId}")
    public ResponseResult Remove(@PathVariable("articleId") Long articleId){
        User user=AppThreadLocalUtil.getUser();

//        User user=new User();
//        user.setId(1302865306489733123L);//测试时把次注解与上方User线程获取互换，设定一个一个死的user_id


        Like like=likeService.query().eq("article_id",articleId).eq("user_id",user.getId()).one();
        likeService.removeById(like.getId());
        return ResponseResult.okResult("删除成功");
    }

    /**
     * 查询收藏√
     * @return
     */

    @GetMapping("/content")
    public ResponseResult queryCollect(){
        List<Like> list = likeService.query().eq("user_id", AppThreadLocalUtil.getUser().getId()).list();
        for (Like like : list){
            Article article=articleService.query().eq("id",like.getArticleId()).one();
            like.setBlogTitle(article.getTitle());

        }

        return ResponseResult.okResult(list);
    }

}
